package com.withoutwords.ui;

import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.settings.Settings;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {

	protected int notificationId = 0;
	protected int soundResId = R.raw.sound_board_down;
	Handler uiHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// Initialize settings prefrences
		Settings settings = Settings.getInstance();
		settings.setPrefrences(this.getApplication().getSharedPreferences(
				"User", Activity.MODE_PRIVATE));
		GameController.getInstance().setApplicatoinContext(
				getApplicationContext());
		uiHandler = new Handler();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
		Common.cancelNotification(this, notificationId);
		setBitmaps();

		GameController.getInstance().setCurrentActivity(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		GameController.getInstance().setCurrentActivity(null);
		unsetBitmaps();

		// TODO Auto-generated method stub

	}

	

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

	}

	MediaPlayer player;

	public void playSound(int soundId) {
		if (soundId != -1) {
			player = MediaPlayer.create(this, soundId);
			int soundVolume = Settings.getInstance().getSoundVolume();
			player.setVolume((float)soundVolume/100, (float)soundVolume/100);
			player.start();
		}
	}

	public void onNewDataRecieved(Intent data) {

	}
	public void OnError(final String errorString)
	{
	uiHandler.post(new Runnable() {
		
		@Override
		public void run() {
			String Msg = BaseActivity.this.getResources().getString(R.string.error_general);
			if (errorString != null)
				Msg = errorString;
			// TODO Auto-generated method stub
			Toast.makeText(BaseActivity.this, Msg, Toast.LENGTH_LONG)
					.show();
			
		}
	});
		
	}

	protected void releaseBitmaps() {

	}

	protected void setBitmaps() {

	}

	protected void unsetBitmaps() {

	}

	protected void setLayoutBitmap(int lId, int bId) {
		ViewGroup ly = (ViewGroup) findViewById(lId);
		if (ly != null) {

			ly.setBackgroundDrawable(BitmapCache.getCacheInstance()
					.getDrawable(this, bId));
			ly.setVisibility(View.VISIBLE);
		}
	}

	protected void unsetLayoutBitmaps(int lId) {
		ViewGroup ly = (ViewGroup) findViewById(lId);
		if (ly != null) {
			ly.setVisibility(View.GONE);
			ly.setBackgroundDrawable(null);
			releaseBitmaps();
		}

	}

	protected void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}
}
